package com.prs.crm.dao.oa.schedule.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.schedule.OthersThingsDao;
import com.prs.crm.domain.oa.schedule.OthersThings;

public class OthersThingsDaoImpl extends BaseDaoImpl<OthersThings> implements OthersThingsDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<OthersThings> getAllOthersThings() {
		return (List<OthersThings>) this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(OthersThings.class);
				return criteria.list();
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OthersThings> findOtherThings(Integer id) {
//		Criteria criteria=this.getSession().createCriteria(OthersThings.class);
//		criteria.createAlias("receivePerson", "rp");
//		criteria.add(Restrictions.eq("rp.id", id));
		Query query=this.getSession().createQuery("from OthersThings as ot where ot.receivePerson.id="+id);
		return query.list();
	}

	@Override
	public OthersThings getTime(final String time) {
		Criteria criteria=this.getSession().createCriteria(OthersThings.class);
		criteria.add(Restrictions.eq("startTime", time));
		List result=criteria.list();
		if (result==null || result.size()==0) {
			return null;
		}
		return (OthersThings) result.get(0);
	}
}
